<?php

namespace backend\controllers;

use Yii;
use backend\models\User;
use backend\models\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * UserController implements the CRUD actions for User model.
 */
class UserController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all User models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new UserSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single User model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new User model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new User();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
    
    
    /* *  
     * 
     *  添加用户
     *
     *  */

    public function actionSignup(){
       
       // 实例化一个表单模型，这个表单模型我们还没有创建，等一下后面再创建
       $model = new \backend\models\SignupForm();
       
        
       if(Yii::$app->request->method != "POST"){
              
            // 渲染添加新用户的表单
            return $this->render('signup', [
                'model' => $model,
            ]);

       } 
        

       // 下面这一段if是我们刚刚分析的第二个小问题的实现，下面让我具体的给你描述一下这几个方法的含义吧
       // $model->load() 方法，实质是把post过来的数据赋值给model的属性
       // $model->signup() 方法, 是我们要实现的具体的添加用户操作
       if ($model->load(Yii::$app->request->post()) && $model->signup()) {
           // 添加完用户之后，我们跳回到index操作即列表页
           return $this->redirect(['index']);
       }
       
    
        
    }
    
    /**
     * Updates an existing User model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        
        $this->layout =false;
//        $model = $this->findModel($id);
        $model = new \backend\models\SignupForm();
        
       
        if (Yii::$app->request->isPost) {
            
             if($model->load(Yii::$app->request->post()) && $model->updateUserInfo($id)){
                 
                 Yii::$app->session->setFlash("success","修改成功");
                 return $this->redirect(['site/index']);
             } else{

                 Yii::$app->session->setFlash("error","修改失败");
                 return $this->redirect(['site/index']);
             }    
 
        } else {
            
            $model_user = User::findOne($id);
       
            $model->username = $model_user->username;
            $model->email = $model_user->email;
        
        
        
            return $this->render('signup', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing User model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the User model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return User the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = User::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    /**
     * 
     * 查找用户的信息 根据ID
     * 
     * @param int $id 用户ID
     * 
     * @return array  返回包含用户信息的 数组
     * **/
    public function actionGetUserInfo($id){
        
        if(empty($id) || !is_int($id)){
            
            return [];
        }
        

        return  User::find()->andWhere(array('id'=>$id))->one();
        
    }
    
    
    
}
